<?php

namespace app\api\service;
use app\admin\model\CouponRecord;
use app\admin\model\User;
use app\common\model\Advance;
use app\common\model\Goodorder;
use app\common\model\member\Order;
use app\common\model\Occupants;
use app\common\model\Payment;
use app\common\model\Reservations;
use app\common\model\Rooms;
use app\common\model\ScoreOrder;
use think\Db;

error_reporting(E_ERROR);
class SignService
{


    /**
     * 结算
     * @return mixed
     */
    public static function Sign_Payment($id,$uid)
    {
        $order=ScoreOrder::where('id','=', $id)->find();
        if (!$order) {
            fail('订单不存在');
        }
        if ($order['pay_status'] != 0) {
            fail('订单状态错误');
        }
        $user_info = User::where('id','=',$uid)->find();
        if (!$user_info || !$user_info['openid']) {
            fail('用户信息不存在，或未绑定微信，请重新登录后再试');
        }
        return \addons\epay\library\Service::submitOrder($order['paid'], $order['order_no'], "wechat",'邮费', request()->domain() . '/api/notify/sign_pay', "", "miniapp",$user_info['openid']);
    }

    /*
     * 支付成功后
     */
    public function order_success($id)
    {

        $payment = ScoreOrder::where('id', $id)->find();
        if ($payment['pay_status'] == 1) {
            fail('该订单已支付');
        }
        $result = ScoreOrder::where('id', $id)->where('pay_status', 0)->update(['pay_status' => 1, 'pay_time' => time()]);
        if ($result == 0) {
            fail('该订单已支付');
        }

        \app\common\model\User::score(-$payment['amount'],$payment['user_id'],'兑换商品');
        $user = User::where('id', $payment['user_id'])->find();

        if ($payment['balance'] > 0) {
            \app\common\model\User::money(-$payment['balance'], $payment['user_id'], '兑换商品邮费');
        }

    }

}